package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.MyCouponEntity;

import java.util.List;

/**
 * Created by Administrator on 2017/6/14.
 */

public class CouponAdapter extends BaseAdapter {
    private Context context;
    private List<MyCouponEntity.DataBean> data;
    private String type;

    public CouponAdapter(Context context, List<MyCouponEntity.DataBean> data, String type) {
        this.context = context;
        this.type = type;
        this.data = data;
    }

    public void refreshData(List<MyCouponEntity.DataBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        Log.e("TAG", "getCount: " + data.size());
        return data != null ? data.size() : 0;
    }

    @Override
    public MyCouponEntity.DataBean getItem(int position) {
        Log.e("TAG", "getItem: " + data.size());
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        couponViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.coupon_item, parent, false);
            holder = new couponViewHolder();
            holder.coupon_condition = (TextView) convertView.findViewById(R.id.coupon_condition);
            holder.coupon_hour = (TextView) convertView.findViewById(R.id.coupon_hour);
            holder.coupon_type = (TextView) convertView.findViewById(R.id.coupon_type);
            holder.coupon_money = (TextView) convertView.findViewById(R.id.coupon_money);
            holder.coupon_linearLayout_background = (LinearLayout) convertView.findViewById(R.id.coupon_linearLayout_background);
            holder.coupon_no_data_background = (LinearLayout) convertView.findViewById(R.id.coupon_no_data_background);
            holder.coupon_no_data_relativeLayout = (RelativeLayout) convertView.findViewById(R.id.coupon_no_data_relativeLayout);
            holder.expire_iv = (ImageView) convertView.findViewById(R.id.expire_iv);
            convertView.setTag(holder);
        } else {
            holder = (couponViewHolder) convertView.getTag();
        }
//        if (data.size() > 0) {
//            holder.coupon_no_data_relativeLayout.setVisibility(View.GONE);
//            holder.coupon_linearLayout_background.setVisibility(View.VISIBLE);
        if (type.equals("1")) {
//            holder.coupon_linearLayout_background.setBackground(context.getDrawable(R.drawable.youhuiquan));
            holder.coupon_linearLayout_background.setBackgroundResource(R.mipmap.youhuiquan);
            holder.expire_iv.setImageResource(R.mipmap.couponmore);
        } else if (type.equals("2")) {
            holder.coupon_linearLayout_background.setBackgroundResource(R.mipmap.youhuiquan_bksy);
            holder.expire_iv.setImageResource(R.mipmap.guoqi);
//            holder.coupon_linearLayout_background.setBackground(context.getDrawable(R.drawable.youhuiquan_bksy));
        }
//        } else {
//            holder.coupon_no_data_relativeLayout.setVisibility(View.VISIBLE);
//            holder.coupon_linearLayout_background.setVisibility(View.GONE);
//        }
        Log.e("TAGT", "getView: " + data.get(position).getETime());
        holder.coupon_condition.setText("满" + data.get(position).getCouponIf() + "元可用");
        holder.coupon_hour.setText(data.get(position).getETime());
        holder.coupon_type.setText(data.get(position).getOpenType());
        holder.coupon_money.setText(data.get(position).getCouponMoney());
        return convertView;
    }

    class couponViewHolder {
        TextView coupon_hour;
        TextView coupon_condition;
        TextView coupon_type;
        TextView coupon_money;
        ImageView expire_iv;
        LinearLayout coupon_linearLayout_background;
        LinearLayout coupon_no_data_background;
        RelativeLayout coupon_no_data_relativeLayout;
    }
}
